package com.tosmart.jorm.cases;

import com.tosmart.jorm.models.Family;
import com.tosmart.jorm.models.Person;
import org.testng.Assert;
import org.testng.annotations.*;

import java.sql.SQLException;
import java.util.List;

/**
 * @author tosmart@gmail.com
 */
public class Reference extends TestBase {

    @BeforeTest
    protected void setUp() throws SQLException {

        super.setUp();

        Family f1 = new Family(1);
        f1.setName("tosmart family");
        f1.setAddress("ChongQing China.");
        f1.setBalance(10000D);

        jormFace.insertEntity(f1);

        Person tosmart = new Person(1);
        tosmart.setName("tosmart");
        tosmart.setSex("M");
        tosmart.setBirth(date("1980-10-22"));
        tosmart.setFamilyId(1);

        jormFace.insertEntity(tosmart);

        Person purong = new Person(2);
        purong.setName("purong");
        purong.setSex("F");
        purong.setBirth(date("1978-09-17"));
        purong.setFamilyId(1);

        jormFace.insertEntity(purong);
    }

    @AfterTest
    protected void tearDown() throws SQLException {
        super.tearDown();
    }

    @Test
    public void testJormRefer() {

        Person tosmart = jormFace.loadEntity(Person.class, 1);
        Assert.assertNotNull(tosmart);

        Family family = tosmart.getFamily();
        Assert.assertNotNull(family);
        Assert.assertEquals(family.getId(), 1);
        Assert.assertEquals(family.getName(), "tosmart family");
        Assert.assertEquals(family.getAddress(), "ChongQing China.");
        Assert.assertEquals(family.getBalance(), 10000D);
    }

    @Test
    public void testJormReferList() {

        Family family = jormFace.loadEntity(Family.class, 1);
        Assert.assertNotNull(family);

        List members = family.getMembers();
        Assert.assertNotNull(members);

        Assert.assertEquals(members.size(), 2);

        Person tosmart = (Person) members.get(0);
        Person purong = (Person) members.get(1);

        Assert.assertEquals(tosmart.getId(), 1);
        Assert.assertEquals(purong.getId(), 2);

        Assert.assertEquals(tosmart.getName(), "tosmart");
        Assert.assertEquals(purong.getName(), "purong");

        Assert.assertEquals(tosmart.getSex(), "M");
        Assert.assertEquals(purong.getSex(), "F");

        Assert.assertEquals(tosmart.getBirth(), date("1980-10-22"));
        Assert.assertEquals(purong.getBirth(), date("1978-09-17"));

        Assert.assertEquals(tosmart.getFamilyId(), 1);
        Assert.assertEquals(purong.getFamilyId(), 1);
    }
}
